In [23]:
import tensorflow as tf
import numpy as np
import gym
In [24]:
import matplotlib.pyplot as plt
%matplotlib inline
In [25]:
sess = tf.InteractiveSession()
In [26]:
e = gym.make('Breakout-v0')
ob = e.reset()
In [27]:
print ob.shape, ob.dtype
plt.imshow(ob)
Out[27]:
In [34]:
# reshape is needed so we can use plt.imshow
rgb_to_gray = tf.reshape(tf.image.rgb_to_grayscale(ob), [ob.shape[0], ob.shape[1]])
In [35]:
gray_ob = rgb_to_gray.eval()
gray_ob.shape, gray_ob.dtype
Out[35]:
In [36]:
plt.gray()
plt.imshow(gray_ob)
Out[36]:
In [39]:
# let's get the current ratio
from __future__ import division
ratio = ob.shape[0] / ob.shape[1]
print ratio
In [87]:
def resize_op(img, h, w):
resized_ob = tf.image.resize_bilinear(tf.reshape(ob, [1, ob.shape[0], ob.shape[1], ob.shape[2]]), [h,w ])
return tf.reshape(resized_ob, [h, w, 3])
In [88]:
# 84 x 84 is the size of the img used in the original DQN paper, 21168 or 7056 (grayscale) points per img
plt.imshow(resize_op(ob, 84, 84).eval())
Out[88]:
In [111]:
w = 40
h = int(w * ratio)
rgb_pixels = 3 * h * w
grayscale_pixels = h * w
print 'height =', h, 'width =', w
print 'pixels with rgb =', rgb_pixels, 'grayscale =', grayscale_pixels
plt.imshow(resize_op(ob, h, w).eval())
Out[111]:
In [92]:
scaled_ob = tf.image.convert_image_dtype(ob, tf.float32, saturate=True).eval()
print scaled_ob.min(), scaled_ob.max()
In [75]:
np.unique(ob)
Out[75]:
In [77]:
np.unique(gray_ob)
Out[77]:
In [94]:
np.unique(scaled_ob)
Out[94]:
In [97]:
# assumes img.shape is (batch_size, h, w, c)
def img_preprocess(img, h, w):
img = tf.convert_to_tensor(img)
rgb2y = tf.image.rgb_to_grayscale(img)
resized = tf.image.resize_bilinear(rgb2y, [h, w])
return resized
In [105]:
obs = np.reshape(ob, [1] + list(ob.shape))
obs.shape
Out[105]:
In [112]:
preprocessed_img = img_preprocess(obs, 84, 84).eval()
In [113]:
plt.gray()
plt.imshow(preprocessed_img.reshape(84, 84))
Out[113]:
We didn't do scaling in the pipeline here if we did we wouldn't be able to show the image. But if we're feeding the input into a neural network we might scale it.
In [ ]: